{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Finding the Users Active Minutes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: findingUsersActiveMinutes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #查找用户活跃分钟数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你用户在 LeetCode 的操作日志，和一个整数 <code>k</code> 。日志用一个二维整数数组 <code>logs</code> 表示，其中每个 <code>logs[i] = [ID<sub>i</sub>, time<sub>i</sub>]</code> 表示 ID 为 <code>ID<sub>i</sub></code> 的用户在 <code>time<sub>i</sub></code> 分钟时执行了某个操作。</p>\n",
    "\n",
    "<p><strong>多个用户 </strong>可以同时执行操作，单个用户可以在同一分钟内执行 <strong>多个操作</strong> 。</p>\n",
    "\n",
    "<p>指定用户的<strong> 用户活跃分钟数（user active minutes，UAM）</strong> 定义为用户对 LeetCode 执行操作的 <strong>唯一分钟数</strong> 。 即使一分钟内执行多个操作，也只能按一分钟计数。</p>\n",
    "\n",
    "<p>请你统计用户活跃分钟数的分布情况，统计结果是一个长度为 <code>k</code> 且 <strong>下标从 1 开始计数</strong> 的数组 <code>answer</code> ，对于每个 <code>j</code>（<code>1 <= j <= k</code>），<code>answer[j]</code> 表示 <strong>用户活跃分钟数</strong> 等于 <code>j</code> 的用户数。</p>\n",
    "\n",
    "<p>返回上面描述的答案数组<i> </i><code>answer</code><i> </i>。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>logs = [[0,5],[1,2],[0,2],[0,5],[1,3]], k = 5\n",
    "<strong>输出：</strong>[0,2,0,0,0]\n",
    "<strong>解释：</strong>\n",
    "ID=0 的用户执行操作的分钟分别是：5 、2 和 5 。因此，该用户的用户活跃分钟数为 2（分钟 5 只计数一次）\n",
    "ID=1 的用户执行操作的分钟分别是：2 和 3 。因此，该用户的用户活跃分钟数为 2\n",
    "2 个用户的用户活跃分钟数都是 2 ，answer[2] 为 2 ，其余 answer[j] 的值都是 0\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>logs = [[1,1],[2,2],[2,3]], k = 4\n",
    "<strong>输出：</strong>[1,1,0,0]\n",
    "<strong>解释：</strong>\n",
    "ID=1 的用户仅在分钟 1 执行单个操作。因此，该用户的用户活跃分钟数为 1\n",
    "ID=2 的用户执行操作的分钟分别是：2 和 3 。因此，该用户的用户活跃分钟数为 2\n",
    "1 个用户的用户活跃分钟数是 1 ，1 个用户的用户活跃分钟数是 2 \n",
    "因此，answer[1] = 1 ，answer[2] = 1 ，其余的值都是 0\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 <= logs.length <= 10<sup>4</sup></code></li>\n",
    "\t<li><code>0 <= ID<sub>i</sub> <= 10<sup>9</sup></code></li>\n",
    "\t<li><code>1 <= time<sub>i</sub> <= 10<sup>5</sup></code></li>\n",
    "\t<li><code>k</code> 的取值范围是 <code>[用户的最大用户活跃分钟数, 10<sup>5</sup>]</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [finding-the-users-active-minutes](https://leetcode.cn/problems/finding-the-users-active-minutes/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [finding-the-users-active-minutes](https://leetcode.cn/problems/finding-the-users-active-minutes/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[0,5],[1,2],[0,2],[0,5],[1,3]]\\n5', '[[1,1],[2,2],[2,3]]\\n4']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        ans = [0] * k\n",
    "        d = {}\n",
    "        for i in logs:\n",
    "            if i[0] in d.keys():\n",
    "                if i[1] not in d[i[0]]:\n",
    "                    d[i[0]].append(i[1])\n",
    "            else:\n",
    "                d[i[0]] = [i[1]]\n",
    "        for i in d:\n",
    "            ans[len(d[i]) - 1] += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# python sort algorithm\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs, k):\n",
    "        zidian={}\n",
    "        res=[0]*k\n",
    "        for id,time in logs:\n",
    "            if id  in zidian:\n",
    "                if time in zidian[id]:\n",
    "                    pass\n",
    "                else:\n",
    "                    zidian[id].append(time)\n",
    "                    changdu=len(zidian[id])\n",
    "                    res[changdu-1]+=1\n",
    "                    res[changdu-2]-=1\n",
    "            else:\n",
    "                zidian[id]=[time]\n",
    "                res[0]+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        search = []\n",
    "        final = [0] * k\n",
    "        for log in logs:\n",
    "            inSearch = False\n",
    "            for i in search:\n",
    "                if i[0] == log[0]:\n",
    "                    if log[1] not in i[1]:\n",
    "                        i[1].append(log[1])\n",
    "                    inSearch = True\n",
    "                    break\n",
    "            if not inSearch:\n",
    "                search.append([log[0], [log[1]]])\n",
    "        for i in search:\n",
    "            final[len(i[1]) - 1] += 1\n",
    "        return final\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        res=[]\n",
    "        for i in range(len(logs)):\n",
    "            if res.count(logs[i])==0:\n",
    "                res+=[logs[i]]\n",
    "        counter=dict()\n",
    "        for log in res:\n",
    "            counter[log[0]]=counter.get(log[0],0)+1\n",
    "        ans=[0]*k\n",
    "        for key in counter:\n",
    "            ans[counter[key]-1]+=1\n",
    "        return(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        se=[0]*k\n",
    "        iu=[]\n",
    "        minute=[]\n",
    "        count=0\n",
    "        logs=sorted(logs,key=lambda x:x[0])\n",
    "        for j,i in enumerate(logs):\n",
    "            if i[0] not in iu and iu==[]:\n",
    "                iu.append(i[0])\n",
    "                minute.append(i[1])\n",
    "                count+=1\n",
    "                if j==len(logs)-1:\n",
    "                    se[count-1]+=1\n",
    "            elif i[0] in iu:\n",
    "                if i[1] not in minute:\n",
    "                    minute.append(i[1])\n",
    "                    count+=1\n",
    "                else:\n",
    "                    count=count\n",
    "                if j==len(logs)-1:\n",
    "                    se[count-1]+=1\n",
    "            else:\n",
    "                se[count-1]+=1\n",
    "                iu=[]\n",
    "                minute=[]\n",
    "                count=0\n",
    "                iu.append(i[0])\n",
    "                minute.append(i[1])\n",
    "                count+=1\n",
    "                if j==len(logs)-1:\n",
    "                    se[count-1]+=1\n",
    "        return se"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        se=[0]*k\n",
    "        iu=[]\n",
    "        minute=[]\n",
    "        count=0\n",
    "        logs=sorted(logs,key=lambda x:x[0])\n",
    "        for j,i in enumerate(logs):\n",
    "            if i[0] not in iu and iu==[]:\n",
    "                iu.append(i[0])\n",
    "                minute.append(i[1])\n",
    "                count+=1\n",
    "                if j==len(logs)-1:\n",
    "                    se[count-1]+=1\n",
    "            elif i[0] in iu:\n",
    "                if i[1] not in minute:\n",
    "                    minute.append(i[1])\n",
    "                    count+=1\n",
    "                else:\n",
    "                    count=count\n",
    "                if j==len(logs)-1:\n",
    "                    se[count-1]+=1\n",
    "            else:\n",
    "                se[count-1]+=1\n",
    "                iu=[]\n",
    "                minute=[]\n",
    "                count=0\n",
    "                iu.append(i[0])\n",
    "                minute.append(i[1])\n",
    "                count+=1\n",
    "                if j==len(logs)-1:\n",
    "                    se[count-1]+=1\n",
    "        return se"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        ls = [0] * k\n",
    "        count = {}\n",
    "        for id, time in logs:\n",
    "            if id in count:\n",
    "                count[id].append(time)\n",
    "            else:\n",
    "                count[id] = [time]\n",
    "        print(count)\n",
    "        res = {}\n",
    "        for key, val in count.items():\n",
    "            min = len(set(val))\n",
    "            if min in res:\n",
    "                res[min] += 1\n",
    "            else:\n",
    "                res[min] = 1\n",
    "\n",
    "        print(res)\n",
    "        for key, val in res.items():\n",
    "            ls[key - 1] = val\n",
    "        print(ls)\n",
    "        return ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        search = []\n",
    "        final = [0] * k\n",
    "        for log in logs:\n",
    "            inSearch = False\n",
    "            for i in search:\n",
    "                if i[0] == log[0]:\n",
    "                    if log[1] not in i[1]:\n",
    "                        i[1].append(log[1])\n",
    "                    inSearch = True\n",
    "                    break\n",
    "            if not inSearch:\n",
    "                search.append([log[0], [log[1]]])\n",
    "        for i in search:\n",
    "            final[len(i[1]) - 1] += 1\n",
    "        return final\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        se=[0]*k\n",
    "        iu=[]\n",
    "        minute=[]\n",
    "        count=0\n",
    "        logs=sorted(logs,key=lambda x:x[0])\n",
    "        for j,i in enumerate(logs):\n",
    "            if i[0] not in iu and iu==[]:\n",
    "                iu.append(i[0])\n",
    "                minute.append(i[1])\n",
    "                count+=1\n",
    "                if j==len(logs)-1:\n",
    "                    se[count-1]+=1\n",
    "            elif i[0] in iu:\n",
    "                if i[1] not in minute:\n",
    "                    minute.append(i[1])\n",
    "                    count+=1\n",
    "                else:\n",
    "                    count=count\n",
    "                if j==len(logs)-1:\n",
    "                    se[count-1]+=1\n",
    "            else:\n",
    "                se[count-1]+=1\n",
    "                iu=[]\n",
    "                minute=[]\n",
    "                count=0\n",
    "                iu.append(i[0])\n",
    "                minute.append(i[1])\n",
    "                count+=1\n",
    "                if j==len(logs)-1:\n",
    "                    se[count-1]+=1\n",
    "        return se"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        D = dict()\n",
    "        n = len(logs)\n",
    "        for user_id, minutes in logs:\n",
    "            if user_id in D:\n",
    "                if minutes in D[user_id]:\n",
    "                    pass\n",
    "                else:\n",
    "                    D[user_id].append(minutes)\n",
    "            else:\n",
    "                D[user_id] = [minutes]\n",
    "        \n",
    "        res = [0 for _ in range(k)]\n",
    "        for _,v in D.items():\n",
    "            number = len(v)\n",
    "            res[number-1] += 1\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        #活跃分钟数->人数\n",
    "        all={}\n",
    "        for i in logs:\n",
    "            if i[0] not in all:\n",
    "                all[i[0]]=[i[1]]\n",
    "            else:\n",
    "                if i[1] not in all[i[0]]:\n",
    "                    all[i[0]].append(i[1])\n",
    "        ret=[]\n",
    "        for i in range(k):\n",
    "            ret.append(0)\n",
    "        for i in all:\n",
    "            ret[len(all[i])-1]+=1\n",
    "        return ret\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        my_hash = defaultdict(set)\n",
    "        ans = [0] * k\n",
    "        for i, t in logs:\n",
    "            my_hash[i].add(t)\n",
    "        \n",
    "        for s in my_hash.values():\n",
    "            ans[len(s) - 1] += 1\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        n = len(logs)\n",
    "        dic = {}\n",
    "        for log in logs:\n",
    "            if log[0] not in dic:\n",
    "                dic[log[0]] = []\n",
    "                dic[log[0]].append(log[1])\n",
    "            else:\n",
    "                if log[1] not in dic[log[0]]:\n",
    "                    dic[log[0]].append(log[1])\n",
    "        ans  = [0] * k\n",
    "        print(\"ans \",ans)\n",
    "        t2u = {}\n",
    "        for k,v in dic.items():\n",
    "            t = len(v)\n",
    "            if t not in t2u:\n",
    "                t2u[t] = 1\n",
    "            else:\n",
    "                t2u[t] = t2u[t] + 1\n",
    "        print(\"t2u \",t2u)\n",
    "        for k,v in t2u.items():\n",
    "            ans[int(k)-1] = int(v)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        ls = [0] * k\n",
    "        count = {}\n",
    "        for id, time in logs:\n",
    "            if id in count:\n",
    "                if time not in count[id]:\n",
    "                    count[id].append(time)\n",
    "            else:\n",
    "                count[id] = [time]\n",
    "        print(count)\n",
    "        res = {}\n",
    "        for key, val in count.items():\n",
    "            min = len(val)\n",
    "            if min in res:\n",
    "                res[min] += 1\n",
    "            else:\n",
    "                res[min] = 1\n",
    "\n",
    "        print(res)\n",
    "        for key, val in res.items():\n",
    "            ls[key - 1] = val\n",
    "        print(ls)\n",
    "        return ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        mp = defaultdict(set)\n",
    "        for i, t in logs:\n",
    "            mp[i].add(t)\n",
    "        ans = [0] * k\n",
    "        for s in mp.values():\n",
    "            ans[len(s) - 1] += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        opts = defaultdict(set)\n",
    "        for id_, t in logs:\n",
    "            opts[id_].add(t)\n",
    "        ans = [0] * k\n",
    "        for opt in opts.values():\n",
    "            ans[len(opt)-1] += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        mp1 = {}\n",
    "        for ID, t in logs:\n",
    "            if(ID not in mp1):\n",
    "                mp1[ID] = set()\n",
    "            mp1[ID].add(t)\n",
    "        result = [0]*k\n",
    "        for ID in mp1:\n",
    "            result[len(mp1[ID])-1] += 1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        answer = [0]*k\n",
    "        dic = defaultdict(set)\n",
    "        for id,time in logs:\n",
    "            dic[id].add(time)\n",
    "        for v in dic.values():\n",
    "            answer[len(v)-1]+=1\n",
    "        return answer\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        res=[0]*k\n",
    "        d=dict()\n",
    "        for x,y in logs:\n",
    "            if x not in d:\n",
    "                d[x]=set()\n",
    "            d[x].add(y)\n",
    "        for x,y in d.items():\n",
    "            res[len(y)-1]+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        res = defaultdict(set)\n",
    "        for x, y in logs:\n",
    "            res[x].add(y)\n",
    "        \n",
    "        ans = [0] * k\n",
    "        for value in res.values():\n",
    "            ans[len(value) - 1] += 1\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        d = defaultdict(set)\n",
    "        for i, t in logs:\n",
    "            d[i].add(t)\n",
    "        ans = [0] * k\n",
    "        for s in d.values():\n",
    "            ans[len(s) - 1] += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        answer = [0]*k\n",
    "        dic = defaultdict(set)\n",
    "        for id,time in logs:\n",
    "            dic[id].add(time)\n",
    "        for key,v in dic.items():\n",
    "            answer[len(v)-1]+=1\n",
    "        return answer\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "\n",
    "        ans = [0] * k\n",
    "        cache = defaultdict(set)\n",
    "\n",
    "        for log in logs:\n",
    "            cache[log[0]].add(log[1])\n",
    "\n",
    "        for user in cache:\n",
    "            ans[len(cache[user]) - 1] += 1\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        idTimeSet = {}\n",
    "\n",
    "        for id,time in logs:\n",
    "            if id in idTimeSet:\n",
    "                idTimeSet[id].add(time)\n",
    "            else:\n",
    "                idTimeSet[id] = set([time])\n",
    "        \n",
    "        res = [0] * k\n",
    "        for id, times in idTimeSet.items():\n",
    "            res[len(times)-1] += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        d = defaultdict(set)\n",
    "        for user, minute in logs:\n",
    "            d[user].add(minute)\n",
    "        ret = [0] * k\n",
    "        for user, s in d.items():\n",
    "            if len(s) <= k:\n",
    "                ret[len(s) - 1] += 1\n",
    "        \n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        answer = [0]*k\n",
    "        dic = defaultdict(set)\n",
    "        for id,time in logs:\n",
    "            dic[id].add(time)\n",
    "        for v in dic.values():\n",
    "            answer[len(v)-1]+=1\n",
    "        return answer\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        a = [0]*k\n",
    "        b = {}\n",
    "        for log in logs:\n",
    "            i,j = log[0],log[1]\n",
    "            if i in b:\n",
    "                b[i].add(j)\n",
    "            else:\n",
    "                b[i]={j}\n",
    "        for i in b.values():\n",
    "            a[len(i)-1] += 1 \n",
    "        return a\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        mp1 = {}\n",
    "        for ID, t in logs:\n",
    "            if(ID not in mp1):\n",
    "                mp1[ID] = set()\n",
    "            mp1[ID].add(t)\n",
    "        result = [0]*k\n",
    "        for ID in mp1:\n",
    "            result[len(mp1[ID])-1] += 1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        res=[0]*k\n",
    "        d=dict()\n",
    "        for x,y in logs:\n",
    "            if x not in d:\n",
    "                d[x]=set()\n",
    "            d[x].add(y)\n",
    "        for x,y in d.items():\n",
    "            res[len(y)-1]+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        \n",
    "        ans = [0]*k\n",
    "        dct = defaultdict(set)\n",
    "        for a, b in logs:\n",
    "            dct[a].add(b)\n",
    "        for a in dct:\n",
    "            ans[len(dct[a])-1] += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        uam_dict = {}  # 记录每个用户的用户活跃分钟数\n",
    "        for log in logs:\n",
    "            uid, minute = log[0], log[1]\n",
    "            if uid not in uam_dict:\n",
    "                uam_dict[uid] = set()\n",
    "            uam_dict[uid].add(minute)\n",
    "        \n",
    "        answer = [0] * k\n",
    "        for uam in uam_dict.values():\n",
    "            count = len(uam)\n",
    "            if count <= k:\n",
    "                answer[count-1] += 1\n",
    "        \n",
    "        return answer\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        d = defaultdict(set)\n",
    "        for i, t in logs:\n",
    "            d[i].add(t)\n",
    "        c = Counter(map(len, d.values()))\n",
    "        ans = [0] * k\n",
    "        for key, value in c.items():\n",
    "            ans[key-1] = value\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "      res = [0 for _ in range(k)]\n",
    "      record = collections.defaultdict(set)\n",
    "      for ids, time in logs:\n",
    "        record[ids].add(time)\n",
    "      for key in record:\n",
    "        if len(record[key]) <= k:\n",
    "          res[len(record[key])-1] += 1\n",
    "      return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        mp = defaultdict(set)\n",
    "        for i, t in logs:\n",
    "            mp[i].add(t)\n",
    "        ans = [0] * k\n",
    "        for s in mp.values():\n",
    "            ans[len(s) - 1] += 1\n",
    "        return ans\n",
    "\n",
    "        \n",
    "\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        idTimeSet = {}\n",
    "\n",
    "        for id,time in logs:\n",
    "            if id in idTimeSet:\n",
    "                idTimeSet[id].add(time)\n",
    "            else:\n",
    "                idTimeSet[id] = set([time])\n",
    "        \n",
    "        res = [0] * k\n",
    "        for times in idTimeSet.values():\n",
    "            res[len(times)-1] += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        # 有点绕\n",
    "        # logs = [[0,5],[1,2],[0,2],[0,5],[1,3]]\n",
    "        # t = {0:{5, 2}, 1:{2, 3}}\n",
    "        # t.values() = [{5,2}, {2,3}], 算出set的长度\n",
    "        # answer = [0, 0, 0, 0, 0] 长度为5\n",
    "\n",
    "        # n = len(logs)\n",
    "        # t = defaultdict(set)\n",
    "        # for idx, minute in logs:\n",
    "        #     t[idx].add(minute)\n",
    "        # # print(t)\n",
    "        # tm = {}\n",
    "        # for x in t.values():\n",
    "        #     print(x)\n",
    "        #     if len(x) in tm:\n",
    "        #         tm[len(x)] += 1\n",
    "        #     else:\n",
    "        #         tm[len(x)] = 1\n",
    "        # # print(tm)\n",
    "        # answer = [0]*k\n",
    "        # for key, v in tm.items():\n",
    "        #     answer[key-1] = v\n",
    "        # return answer\n",
    "\n",
    "\n",
    "        # 官解\n",
    "        mp = defaultdict(set)\n",
    "        for i, t in logs:\n",
    "            mp[i].add(t)\n",
    "        answer = [0]*k \n",
    "        for s in mp.values():\n",
    "            answer[len(s) - 1] += 1  # 用累加，少一个循环\n",
    "        return answer\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        d = defaultdict(set)\n",
    "        for x, y in logs:\n",
    "            d[x].add(y)\n",
    "        ans = [0] * k\n",
    "        for v in d.values():\n",
    "            ans[len(v) - 1] += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        res = [0]*k\n",
    "        id_map = defaultdict(set)\n",
    "        for id, time in logs:\n",
    "            id_map[id].add(time)\n",
    "        for _, time_set in id_map.items():\n",
    "            res[len(time_set)-1] += 1\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        ans = [0] * k\n",
    "        cache = defaultdict(set)\n",
    "\n",
    "        for log in logs:\n",
    "            cache[log[0]].add(log[1])\n",
    "\n",
    "        for user in cache:\n",
    "            ans[len(cache[user]) - 1] += 1\n",
    "\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        各人的活跃分钟数 = dict()\n",
    "        for i, j in logs:\n",
    "            if i in 各人的活跃分钟数:\n",
    "                各人的活跃分钟数[i].add(j)\n",
    "            else:\n",
    "                各人的活跃分钟数[i] = {j}\n",
    "        返回 = [0]*k\n",
    "        for i in 各人的活跃分钟数.values():\n",
    "            返回[len(i)-1] += 1\n",
    "        return 返回"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        mp = defaultdict(set)\n",
    "        for i, t in logs:\n",
    "            mp[i].add(t)\n",
    "        ans = [0] * k\n",
    "        for s in mp.values():\n",
    "            ans[len(s) - 1] += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        ans = [0] * k\n",
    "        dic = defaultdict(set)\n",
    "        for i, t in logs:\n",
    "            dic[i].add(t)\n",
    "        for v in dic.values():\n",
    "            ans[len(v) - 1] += 1\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        d = defaultdict(set)\n",
    "        for x, y in logs:\n",
    "            d[x].add(y)\n",
    "        ans = [0] * k\n",
    "        for v in d.values():\n",
    "            ans[len(v) - 1] += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        d = defaultdict(set)\n",
    "        for x, y in logs:\n",
    "            d[x].add(y)\n",
    "        ans = [0] * k\n",
    "        for v in d.values():\n",
    "            ans[len(v) - 1] += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        answer = [0 for _ in range(k)]\n",
    "        logs_dict = {}\n",
    "        for i, n in logs:\n",
    "            if i not in logs_dict.keys():\n",
    "                logs_dict[i] = set()\n",
    "            logs_dict[i].add(n)\n",
    "        for i in logs_dict.keys():\n",
    "            answer[len(logs_dict[i])-1] += 1\n",
    "        return answer\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        res = dict()\n",
    "        for x,y in logs:\n",
    "            if x in res:\n",
    "                res[x].add(y)\n",
    "            else:\n",
    "                res[x] = {y}\n",
    "        temp = []\n",
    "        for value in res.values():\n",
    "            temp.append(len(value))\n",
    "        ans = [0] * k\n",
    "        for i,j in Counter(temp).items():\n",
    "            if i <= k:\n",
    "                ans[i - 1] = j\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\r\n",
    "        ans = [0] * k\r\n",
    "        d = defaultdict(set)\r\n",
    "        for i, j in logs:\r\n",
    "            d[i].add(j)\r\n",
    "        for i in d:\r\n",
    "            ans[len(d[i])-1] += 1\r\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        count = defaultdict(set)\n",
    "        ls = [0] * k\n",
    "\n",
    "        for id, time in logs:\n",
    "            count[id].add(time)\n",
    "        for val in count.values():\n",
    "            ls[len(val) - 1] += 1\n",
    "        print(ls)\n",
    "        return ls\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # * 哈希表\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        mp = defaultdict(set)\n",
    "        for i, t in logs:\n",
    "            mp[i].add(t)\n",
    "\n",
    "        ans = [0] * k\n",
    "        for s in mp.values():\n",
    "            ans[len(s) - 1] += 1\n",
    "\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        uam_dict = defaultdict(set)  # 记录每个用户的用户活跃分钟数\n",
    "        for log in logs:\n",
    "            uid, minute = log[0], log[1]\n",
    "            uam_dict[uid].add(minute)\n",
    "        \n",
    "        answer = [0] * k\n",
    "        for uam in uam_dict.values():\n",
    "            count = len(uam)\n",
    "            if count <= k:\n",
    "                answer[count-1] += 1\n",
    "        \n",
    "        return answer\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        ans = [0]*k\n",
    "        user = defaultdict(set)\n",
    "        temp =[]\n",
    "        for i in logs:\n",
    "            user[i[0]].add(i[1])\n",
    "        for i in user.values():\n",
    "            ans[len(set(i)) - 1] += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        ans=[0]*k\n",
    "        hash=defaultdict(set)\n",
    "        for a,b in logs:\n",
    "            hash[a].add(b)\n",
    "        cnt=Counter([len(hash[x]) for x in hash])\n",
    "        for x in cnt:\n",
    "            ans[x-1]=cnt[x]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        # 去重\n",
    "        logs = list([j[0] for j in set((i[0], i[1]) for i in logs)])\n",
    "        each_one_use_time = dict(collections.Counter(logs))\n",
    "        each_sum_time = list(each_one_use_time.values())\n",
    "        mins = dict(collections.Counter(each_sum_time))\n",
    "\n",
    "        result = []\n",
    "        for i in range(1,k+1):\n",
    "            if mins.get(i) == None:\n",
    "                result.append(0)\n",
    "            else:\n",
    "                result.append(mins.get(i))\n",
    "                \n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        user_t = defaultdict(set)\n",
    "        for u, t in logs:\n",
    "            user_t[u].add(t)\n",
    "        \n",
    "        ans = [0] * k\n",
    "        for t in user_t.values():\n",
    "            ans[len(t)-1] += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        user = dict()\n",
    "        l = []\n",
    "        ans = [0] * k\n",
    "        for i in range(len(logs)):\n",
    "            if logs[i][0] in user.keys():\n",
    "                user[logs[i][0]].add(logs[i][1])\n",
    "            else:\n",
    "                user[logs[i][0]] = {logs[i][1]}\n",
    "        for i in user.values():\n",
    "\n",
    "            ans[len(i)-1] +=1\n",
    "        return ans\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        logSet = set((tuple(item) for item in logs))\n",
    "        persons = {}\n",
    "        for item in logSet:\n",
    "            persons[item[0]] = persons.get(item[0], 0) + 1\n",
    "        days = {}\n",
    "        for person in persons:\n",
    "            day = persons[person]\n",
    "            days[day] = days.get(day, 0) + 1\n",
    "        result = []\n",
    "        for i in range(1, k+1):\n",
    "            result.append(days.get(i, 0))\n",
    "        return result\n",
    "        \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        idMinSet = {}\n",
    "\n",
    "        for id,time in logs:\n",
    "            if id in idMinSet:\n",
    "                idMinSet[id].add(time)\n",
    "            else:\n",
    "                idMinSet[id] = set([time])\n",
    "        \n",
    "        countIdArr = {}\n",
    "        for id, minSet in idMinSet.items():\n",
    "            minutes = len(minSet)\n",
    "            if minutes in countIdArr:\n",
    "                countIdArr[minutes].append(id)\n",
    "            else:\n",
    "                countIdArr[minutes] = [id]\n",
    "        \n",
    "        res = []\n",
    "        for i in range(1, k+1):\n",
    "            res.append(len(countIdArr.get(i, [])))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        mp=defaultdict(set)\n",
    "        for i,t in logs:\n",
    "            mp[i].add(t)\n",
    "        ans=[0]*k\n",
    "        for s in mp.values():\n",
    "            ans[len(s)-1]+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        from collections import defaultdict\n",
    "        dic = defaultdict(set)\n",
    "        for log in logs:\n",
    "            dic[log[0]].add(log[1])\n",
    "        \n",
    "        res = [0 for _ in range(k)]\n",
    "        for v in dic.values():\n",
    "            res[len(v)-1] += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\r\n",
    "        from collections import defaultdict\r\n",
    "        user_time_map = defaultdict(set)\r\n",
    "        for user, time in logs:\r\n",
    "            user_time_map[user].add(time)\r\n",
    "        user_time_map = {i:len(user_time_map[i]) for i in user_time_map}\r\n",
    "        answers = [0] * k\r\n",
    "        for _, time in user_time_map.items():\r\n",
    "            answers[time - 1] += 1\r\n",
    "        return answers\r\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\r\n",
    "        dic = {}\r\n",
    "        for id, time in logs:\r\n",
    "            if id in dic:\r\n",
    "                dic[id].add(time)\r\n",
    "            else:\r\n",
    "                dic[id] = set([time])\r\n",
    "        ans = [0 for _ in range(k)]\r\n",
    "        for key in dic:\r\n",
    "            ans[len(dic[key]) - 1] += 1\r\n",
    "        return(ans)\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\r\n",
    "        dic = {}\r\n",
    "        for id, time in logs:\r\n",
    "            if id in dic:\r\n",
    "                dic[id].add(time)\r\n",
    "            else:\r\n",
    "                dic[id] = set([time])\r\n",
    "        ans = [0 for _ in range(k)]\r\n",
    "        for key in dic:\r\n",
    "            ans[len(dic[key]) - 1] += 1\r\n",
    "        return(ans)\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        uniq_logs = set([tuple(log) for log in logs])\n",
    "\n",
    "        cnt = {}\n",
    "        for log in uniq_logs:\n",
    "            if log[0] not in cnt:\n",
    "                cnt[log[0]] = 1\n",
    "            else:\n",
    "                cnt[log[0]] += 1\n",
    "        \n",
    "        res = [0] * k\n",
    "        for c in cnt:\n",
    "            v = cnt[c]\n",
    "            res[v-1] += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        # 有点绕\n",
    "        # logs = [[0,5],[1,2],[0,2],[0,5],[1,3]]\n",
    "        # t = {0:{5, 2}, 1:{2, 3}}\n",
    "        # t.values() = [{5,2}, {2,3}], 算出set的长度\n",
    "        # answer = [0, 0, 0, 0, 0] 长度为5\n",
    "        \n",
    "        n = len(logs)\n",
    "        t = defaultdict(set)\n",
    "        for idx, minute in logs:\n",
    "            t[idx].add(minute)\n",
    "        # print(t)\n",
    "        tm = {}\n",
    "        for x in t.values():\n",
    "            print(x)\n",
    "            if len(x) in tm:\n",
    "                tm[len(x)] += 1\n",
    "            else:\n",
    "                tm[len(x)] = 1\n",
    "        # print(tm)\n",
    "        answer = [0]*k\n",
    "        for key, v in tm.items():\n",
    "            answer[key-1] = v\n",
    "        return answer\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        answer = [0]*k\n",
    "        dic = defaultdict(set)\n",
    "        for id,time in logs:\n",
    "            dic[id].add(time)\n",
    "        print(dic)\n",
    "        for key,v in dic.items():\n",
    "            answer[len(v)-1]+=1\n",
    "        return answer\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findingUsersActiveMinutes(self, logs: List[List[int]], k: int) -> List[int]:\n",
    "        idmap = {}\n",
    "        for log in logs:\n",
    "            i, minute = log[0], log[1]\n",
    "            if i not in idmap.keys():\n",
    "                idmap[i] = set()\n",
    "            idmap[i].add(minute)\n",
    "        res = [0]*(k+1)\n",
    "        for i, ms in idmap.items():\n",
    "            res[len(ms)] += 1\n",
    "        return res[1:]"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
